#!/bin/bash -e

log=.log.`date +%N`
echo `basename $0` $@ >>$log || exit 0;
prog=`basename $0`
args=$@

function info() {
cat /mnt/ilustre/app/medical/tools/script/info.snp.txt
rm $log
exit 1
}

function error() {
echo `basename $0`': invalid option -?'
info
}


while getopts  ":a:s:i:l:d:gkLGnc:Ap:m:q:r:t:vF:D:M" opts
do
        case  $opts  in
		a) cutx=$OPTARG;;
        i) interval=$OPTARG
			i=i;;
		l) target=$OPTARG
			f=f;;
		g) single_cell=T;;
		G) somatic=T;;
		d) desti=$OPTARG;;
		k) mark=T;;
		n) cutadaptor=$OPTARG
			n=n;;
		c) cnv=$OPTARG
			c=c;;
		r) realn_interval=$OPTARG;;
		m) mem=$OPTARG
			m=m;;
		p) cpu=$OPTARG
			p=p;;
		q) query=$OPTARG
			q=q;;
		A) a=n;;
		s) sample_name=$OPTARG;;
		L) run_local=T;;
		t) continue_dir=$OPTARG;;
		v) cov=T;;
        M) mixture=T;;
        F) depth_threshhold=$OPTARG;;
        D) depth_design=$OPTARG;;
		h) info;;
		\?) error;;
        esac
done
shift $(($OPTIND - 1))

if [ -z "$1" ]; then info; fi

if [ -z "$sample_name" ]; then
if test -d $1; then
sample_name=`fq_name.pl $1`;
if test -z "$sample_name"; then sample_name=test; fi
else sample_name=test
fi
fi

if [ -z "$desti" ]; then desti=snp; fi

if test "$(id -u)" != "577"; then
desti=snp/$(id -un)

fi

if [ -z "$cutx" ]; then cutx=malbac; fi
if [ -z "$query" ]; then query=$(id -gn); fi
if test "$query" = "medical"; then query=medical; fi

if test -z "$run_local"; then
	if test -z "$cpu"; then
		if test -z "$mem"; then
			subq.sh -n$sample_name -q$query $prog -L $args
		else
			subq.sh -n$sample_name -m$mem -q$query $prog -L $args
		fi
	else
		if test -z "$mem"; then
			subq.sh -n$sample_name -p$cpu -q$query $prog -L $args
		else
			subq.sh -n$sample_name -m$mem -q$query -p$cpu $prog -L $args
		fi
	fi

clear
echo A snp calling and annotion job, name $sample_name, submitted to pbs...
exit 0
fi

if
echo $1 |perl -n -e 'if(/^\//){exit 0;}else{exit 2;}'
then
a=$1
else
a=`pwd`/$1
fi

if test -n "$2"; then


if
echo $2 |perl -n -e 'if(/^\//){exit 0;}else{exit 2;}'
then
b=$2
else
b=`pwd`/$2
fi


# b=`pwd`/$2
r12=`echo $a $b`
# echo r12 $r12
elif test -d $1; then
num=`ls -1 $1|wc -l`
	if test $num -eq 2; then
	ab=`ls $a`
	aa=`echo $ab |cut -d' ' -f1`
	bb=`echo $ab |cut -d' ' -f2`
	r12=`echo $a/$aa $a/$bb`
	# echo r12 $r12
	fi
fi

path_abs=path_abs.sh

r1=`$path_abs $1`
r2=`$path_abs $2`
r12=`echo $r1 $r2`

if test -z "$continue_dir"; then
makedir=true
else
echo no make dir
cd $continue_dir
fi


. $var

mnt.sh $$ `basename $0 .sh`.`basename $(pwd)` &
mnt_pid=$!


if [ -n "$p" ]; then
	qsub_cmd="$qsub_cmd -l nodes=1:ppn=$cpu"
fi

if [ -n "$m" ]; then
	qsub_cmd="$qsub_cmd -l mem=${mem}gb"
fi

if [ -n "$q" ]; then
	qsub_cmd="$qsub_cmd -$q $query"
fi

if test "$cutx" = "malbac"; then
	cutadp=cutadp_malbac_170214.sh
    # cutadp=cutadp_malbac_ctc80.sh
elif test "$cutx" = "general"; then
	cutadp=cutadp_170214.sh
fi


if [ "$interval" = "brca12" ]; then
	target=$data_path/intervals/1/brca.qiagen.NGHS-102X-Covered.b37.bed
elif [ "$interval" = "brca44" ]; then
	target=$data_path/intervals/1/brca.qiagen.NGHS-001X-Covered.b37.bed
elif [ "$interval" = "brca12mor" ]; then
	cutadp=cutadp_ph_miseq.sh
	target=$data_path/intervals/1/brca12_mor.index.bed
elif [ "$interval" = "brca12ph" ]; then
	cutadp=cutadapt_brca12_ph.sh
	target=$data_path/intervals/1/brca12_b37_sort_ph.index.bed
elif [ "$interval" = "ca50mor" ]; then
	cutadp=cutadp_ph_miseq.sh
	target=$data_path/intervals/1/ca50_mor.bed

elif [ "$interval" = "ctc80" ]; then
	cutadp=cutctc80.sh
	target=$data_path/intervals/1/ctc80.index.bed
elif [ "$interval" = "exomes" ]; then
    cutadp=cutadp_a_m.sh
	target=$data_path/intervals/1/exon.b37.index.bed
elif [ "$interval" = "wg" ]; then
    cutadp=cutadp_a_m.sh
	target=$data_path/intervals/1/b37.bed
elif [ "$interval" = "tmp" ]; then
    cutadp=cutadp_a_m.sh
	target=$data_path/intervals/1/probe3.b37.pade0.sort.bed
else
	cutadp=cutadp_170214.sh
fi

# if test "$cutx" = "malbac" -a "$interval" = "ctc80" -a -n "$single_cell"; then
    # cutadp=cutadp_malbac_ctc80.sh
# fi


echo;echo interval: $target
echo $cutadp

# http='http://192.168.10.113:8000'

snapshot=$out_prefix.snapshot.txt
test -e $snapshot || echo -e "Sample\t$sample_name\nRaw data\t$r12\nPanel\t$interval\nResults\t${http}/$desti/" >$snapshot


if test ${cpu_num:=8} -gt 1; then
ts0=`echo ${cpu_num}-3|bc`
else
ts0=1
fi

fastqstat=fastqstat.sh
if
cat $pid_log|grep "$fastqstat $r12 done" >/dev/null; then
echo $fastqstat done
else
fastqstat.sh -s$sample_name -t2 -p$out_prefix $r12
fi


# great or equal than 1;
# because fastqc would run more than one time,
# if it runs 2 times, then it works.
fastqc=fastqc.sh
# if test `cat $pid_log|grep "^$fastqc done" |wc -l` -ge 1; then
if
cat $pid_log|grep "$fastqc $r12 done" >/dev/null; then
echo $fastqc done
else
$fastqc -s$sample_name $r12
fi

if
cat $pid_log|grep "$cutadp $r12 done" >/dev/null; then
echo $cutadp done
else
$cutadp -p$out_prefix $r12
fi
# align0223.sh -p$out_prefix -s$sample_name $out_prefix.1.cut.fastq $out_prefix.2.cut.fastq
aln=aln_bwa_bt.sh
if
# cat $pid_log|grep "$aln done" >/dev/null; then
cat $pid_log|grep "$aln $out_prefix.1.cut.fastq $out_prefix.2.cut.fastq done" >/dev/null; then
echo aln_bwa_bt.sh done
else
$aln -t$ts0 -s$sample_name $out_prefix.1.cut.fastq $out_prefix.2.cut.fastq && rm $out_prefix.1.cut.fastq $out_prefix.2.cut.fastq
fi

:<<sam2b
sam2b=sam2b
sam2bs=sam2b_samtools.sh
sam2bp=sam2b_picard.sh

if
# cat $pid_log|grep "^$sam2b.* done" >/dev/null; then
cat $pid_log|grep "$sam2b $out_prefix.sam done" >/dev/null; then
echo $sam2b done
else

if test `du $out_prefix.sam|cut -f1` -lt 1000000; then

sam2b_picard.sh $out_prefix.sam
else

sam2b_samtools.sh -t$ts0 $out_prefix.sam

fi

fi
sam2b

sam2b=sam2b_samtools.sh
if
cat $pid_log|grep "$sam2b $out_prefix.sam done" >/dev/null; then
		echo $sam2b $out_prefix.sam done
	else
		$sam2b -t$ts0 $out_prefix.sam
fi

summ=summ.sh

test -n "$cov" && $summ -s$sample_name -g $out_prefix.sort.bam $target && . $cmd_done

# markdup
if test "$mark" = "T"; then
deldup=deldup.sh
	if
	# cat $pid_log|grep "^$deldup done" >/dev/null; then
	cat $pid_log|grep "$deldup $out_prefix.sort.bam done" >/dev/null; then
	echo $deldup done
	else
		# delete symbolic link if exists.
		if test -e $out_prefix.markdup.bam; then
			rm $out_prefix.markdup.bam*;
		fi

		$deldup -m $out_prefix.sort.bam
	fi

else
	ln -fs $out_prefix.sort.bam $out_prefix.markdup.bam
	if test -e $out_prefix.sort.bam.bai; then
		ln -fs $out_prefix.sort.bam.bai $out_prefix.markdup.bam.bai
	elif test -e $out_prefix.sort.bai; then
		ln -fs $out_prefix.sort.bai $out_prefix.markdup.bai
	fi

fi
# end markdup

# cnv
if test -n "$cnv"; then

	if test "$cnv" = "hmmcp"; then

	deldup=deldup.sh
	if
	# test -e "$out_prefix.markdup.bam"; then
	cat $pid_log|grep "$deldup $out_prefix.markdup.bam done" >/dev/null; then
	echo $deldup done
	else
	deldup.sh $out_prefix.sort.bam
	fi

	hmmcopy0917b38.sh -w50000 -b37 $out_prefix.deldup.bam
	hmmcopy0917b38.sh -w200000 -b37 $out_prefix.deldup.bam
	hmmcopy0917b38.sh -w1000000 -b37 $out_prefix.deldup.bam
	fi
fi
#end cnv

# fastqc.sh -s$sample_name $out_prefix.sort.bam &

realign_bt=realign_bt.sh
if
# cat $pid_log|grep "^$realign_bt done" >/dev/null; then
cat $pid_log|grep "$realn $out_prefix.markdup.bam done" >/dev/null; then
echo $realign_bt done
else

# realign_bt.sh $out_prefix.markdup.bam
if test -n "$realn_interval"; then

if test -n "$target"; then
echo realign with known interval $realn_interval in target $target
realign_bt.sh -r$realn_interval -i$target $out_prefix.markdup.bam
else
realign_bt.sh -r$realn_interval $out_prefix.markdup.bam
fi


elif test -n "$target"; then
echo realign in $target
realign_bt.sh -i$target $out_prefix.markdup.bam
else
echo realign in whole region
realign_bt.sh $out_prefix.markdup.bam
fi

fi

recal=recal.sh
if
# cat $pid_log|grep "^$recal done" >/dev/null; then
cat $pid_log|grep "$recal done" >/dev/null; then
echo $recal done
else

recal.sh -p1 -i$target
fi

if
# test `cat $pid_log|grep "$fastqc done" |wc -l` -ge 2; then
cat $pid_log|grep "$fastqc $out_prefix.realn.recal.bam done" >/dev/null; then
echo $fastqc done
else
$fastqc -s$sample_name $out_prefix.realn.recal.bam
fi

# cal0=cal.sh
# if
# cat $pid_log|grep "^$cal0 done" >/dev/null; then
# echo $cal0 done
# else
# cal.sh -p1 -i$target
# fi

# calling
cal0=ug_easy.sh
if
cat $pid_log|grep "$cal0 $out_prefix.realn.recal.bam done" >/dev/null; then
echo $cal0 $out_prefix.realn.recal.bam done
else
	if test -n "$somatic"; then
		echo somatic or low coverage
        if test -n "$single_cell"; then
            echo single cell
            $cal0 -G1 -p$out_prefix -i$target -d${depth_design:=30} $out_prefix.realn.recal.bam
        else
            $cal0 -G -p$out_prefix -i$target -d${depth_design:=30} $out_prefix.realn.recal.bam
		fi
	else
        if test -n "$single_cell"; then
            $cal0 -1 -p$out_prefix -i$target -d${depth_design:=30} $out_prefix.realn.recal.bam
        else
            $cal0 -p$out_prefix -i$target -d${depth_design:=30} $out_prefix.realn.recal.bam
        fi
	fi
fi

summ=summ.sh
if
# cat $pid_log|grep "^$summ done" >/dev/null; then
cat $pid_log|grep "$summ $out_prefix.realn.recal.bam $target done" >/dev/null; then
echo $summ done
else
$summ -g -s$sample_name $out_prefix.realn.recal.bam $target #can not background

ln $out_prefix.gene_summ.txt gene_summ.txt
fi

# anno=anno_0216.sh
anno=anno_110416.sh
if
# cat $pid_log|grep "^$anno done" >/dev/null; then
cat $pid_log|grep "$anno $out_prefix.snp.vcf $out_prefix.indel.vcf done" >/dev/null; then
echo $anno done
else

if test -n "$somatic"; then
	echo somatic
	if test -n "$single_cell"; then
		echo;echo;echo single_cell
		$anno -m -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	else
		$anno -m -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	fi
else
	echo germline
	if test -n "$single_cell"; then
		echo;echo;echo single_cell
		$anno -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	else
		$anno -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	fi
fi

fi

format_simple=format_top_simple.sh
if
# cat $pid_log|grep "^$format_simple done" >/dev/null; then
cat $pid_log|grep "$format_simple done" >/dev/null; then
echo $format_simple done
else
format_top_simple.sh
fi

format=format_top_0222.sh
if
# cat $pid_log|grep "^$format done" >/dev/null; then
cat $pid_log|grep "$format $out_prefix.high.vcf $out_prefix.low.vcf done" >/dev/null; then
echo $format done
else
# format_top_0508.sh
format_top_0222.sh $out_prefix.high.vcf $out_prefix.low.vcf
fi

# format_jxl.sh 1.xls 1.jxl.xls


# wait # mnt compatible with wait.


rename=file_rename_0222.sh
if
# cat $pid_log|grep "^$rename done" >/dev/null; then
cat $pid_log|grep "$rename $sample_name done" >/dev/null; then
echo $rename done
else
file_rename_0222.sh $sample_name
fi



echo;echo; echo desti: $desti

:<<BK
if
ls *.pdf
then
bk.sh -t$desti *.pdf *.html *.xls .log* *.txt *.vcf
else
bk.sh -t$desti *.txt *.html *.xls .log* *.vcf
fi
BK

bk.sh -tproject/$desti ${sample_name}*variant*.xls \
${sample_name}*simple_info*.xls \
${sample_name}*summary*.xls \
*stat*.xls \
*.pdf \
${sample_name}*variants*.html \
${sample_name}*deleterious.txt \
${sample_name}*impact*drugs-prediction* \
.log* .pig.log

# bk_zip.sh ${sample_name}*anno*.xls ${sample_name}*summary*.xls ${sample_name}*stat*.xls ${sample_name}*reads*.xls ${sample_name}*.pdf ${sample_name}*2*.html

bk_zip.sh ${sample_name}*variant*.xls \
${sample_name}*simple_info*.xls \
${sample_name}*summary*.xls \
*stat*.xls \
*.pdf \
${sample_name}*variants.2.html \
${sample_name}*impact*drugs-prediction.html



# mail.sh $sample_name $interval $desti/`basename $(pwd)` $r12


. $cmd_done
